package com.kzn.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kzn.entity.Crowd;
import com.kzn.service.ICrowdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sorclown
 * @since 2022-02-05
 */
@RestController
@RequestMapping("/api/crowd")
public class CrowdController {
    private ICrowdService crowdService;

    public CrowdController() {
    }

    @Autowired
    public CrowdController(ICrowdService crowdService) {
        this.crowdService = crowdService;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public boolean register(@RequestBody Crowd crowd) {
        if (null != crowd) {
            return crowdService.save(crowd);
        } else {
            return false;
        }
    }

    @RequestMapping(value = "/logOut", method = RequestMethod.GET)
    public boolean logOut(@RequestParam Integer crowdId) {
        if (null != crowdId) {
            QueryWrapper<Crowd> crowdQueryWrapper = new QueryWrapper<>();
            crowdQueryWrapper.eq("id", crowdId);
            return crowdService.remove(crowdQueryWrapper);
        } else {
            return false;
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public boolean update(@RequestBody Crowd crowd) {
        if (null != crowd.getId()) {
            return crowdService.updateById(crowd);
        } else {
            return false;
        }
    }

    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public String selectById(@RequestParam Integer crowdId) {
        QueryWrapper<Crowd> wrapper = new QueryWrapper<>();
        wrapper.eq("id", crowdId);
        Crowd crowd = crowdService.getOne(wrapper);
        if (null != crowd) {
            return JSONUtil.parse(crowd).toString();
        } else {
            return null;
        }
    }

    @RequestMapping(value = "/logIn", method = RequestMethod.GET)
    public String logIn(@RequestParam String phone, @RequestParam String password) {
        QueryWrapper<Crowd> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        Crowd crowd = crowdService.getOne(wrapper);
        if (crowd != null && phone.equals(crowd.getPhone()) && password.equals(crowd.getPassword()))
        {
            return JSONUtil.parse(crowd).toString();
        }else {
            return null;
        }
    }

}


